﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Unitity.Storage;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Interface;

namespace Spinning.Spinning.Scenes.SinglePlayer
{
    class CalRewardScene : BaseScene
    {
        String titleTextureName = "";
        StageScoreData data;
        TextEntity timeText, scoreText, speedText, bounsText, totalText;
        Sprite timeSprite, scoreSprite, speedSprite, bounsSprite, totalSprite, line;
        List<Sprite> lineList = new List<Sprite>();
        int timeValue;

        public override void OnLoad()
        {
            base.OnLoad();
            data = Game1.stageManager.stageScoreData;

            //setTempData();

            if (data.isWin == true)
            {
                titleTextureName = "singleReward/txt_win";
                if (data.Time < 30)
                    timeValue = 40;
                else if (data.Time < 60)
                    timeValue = 20;
                else
                    timeValue = 0;
                if (data.StageNumber < 4&&!data.isBattle)//1 2 3 
                {
                   // if (data.StageNumber < Game1.stageManager.GameSrate.isLockedGameStage.Count)
                        Game1.stageManager.GameSrate.isLockedGameStage[data.StageNumber] = false;
                }
            }
            else
            {
                titleTextureName = "singleReward/txt_lose";
            }
            Sprite backGroundSprite = new Sprite();
            backGroundSprite.textureName = "bg";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            Sprite itemTitleSprite = new Sprite();
            itemTitleSprite.textureName = titleTextureName;
            itemTitleSprite.ZIndex = Constants.ui;
            itemTitleSprite.OnLoad();
            itemTitleSprite.Position = new Vector2(170, 60);
            this.Attach(itemTitleSprite);

            Sprite barSprite = new Sprite();
            barSprite.textureName = "item_store/bar";
            barSprite.ZIndex = Constants.uibackground;
            barSprite.OnLoad();
            barSprite.Position = new Vector2(75, 40);
            this.Attach(barSprite);

            Sprite blockSprite = new Sprite();
            blockSprite.textureName = "singleReward/block_06";
            blockSprite.ZIndex = Constants.uibackground;
            blockSprite.OnLoad();
            blockSprite.Position = new Vector2(27, 180);
            this.Attach(blockSprite);

            timeSprite = new Sprite();
            timeSprite.textureName = "singleReward/txt_lowercase_time";
            timeSprite.ZIndex = Constants.ui;
            timeSprite.OnLoad();
            this.Attach(timeSprite);

            scoreSprite = new Sprite();
            scoreSprite.textureName = "singleReward/txt_lowercase_score";
            scoreSprite.ZIndex = Constants.ui;
            scoreSprite.OnLoad();
            this.Attach(scoreSprite);

            speedSprite = new Sprite();
            speedSprite.textureName = "singleReward/txt_lowercase_speed";
            speedSprite.ZIndex = Constants.ui;
            speedSprite.OnLoad();
            this.Attach(speedSprite);

            bounsSprite = new Sprite();
            bounsSprite.textureName = "singleReward/txt_lowercase_bouns";
            bounsSprite.ZIndex = Constants.ui;
            bounsSprite.OnLoad();
            this.Attach(bounsSprite);

            totalSprite = new Sprite();
            totalSprite.textureName = "singleReward/txt_lowercase_total";
            totalSprite.ZIndex = Constants.ui;
            totalSprite.OnLoad();
            this.Attach(totalSprite);

            Sprite starSprite = new Sprite();
            starSprite.textureName = "item_store/star_s";
            starSprite.ZIndex = Constants.uibackground;
            starSprite.OnLoad();
            starSprite.Position = new Vector2(70, 600);
            this.Attach(starSprite);

            for (int i = 0; i < 2; i++)
            {
                line = new Sprite();
                line.textureName = "item_store/line";
                line.OnLoad();
                line.ZIndex = Constants.ui;
                Attach(line);
                line.Origin = new Vector2(.5f);
                lineList.Add(line);

            }
            makeTextEntity();

            if (!data.isBossStage&&!data.isBattle) //非boss關 排版
            {
                setStageReward();
            }
            else
            {
                setBossReward();
            }

            totalText.Position = new Vector2(270, 590);
            totalSprite.Position = new Vector2(90, 590);

            int total = timeValue + data.Score + data.Speed + data.Bouns;
            totalText.Value = "+" + total;

            Game1.stageManager.GameSrate.TotalScore += total;
            Vector2 v2 = new Vector2(295, 692);
            if (data.isBattle)
            {
                v2 = new Vector2(385, 695);
            }
            else 
            {
                ButtonSprite okSprite = new ButtonSprite();
                okSprite.textureName = "singleReward/ok";
                okSprite.ZIndex = Constants.ui;
                okSprite.Position = new Vector2(385, 695);
                okSprite.OnLoad();
                TouchManager.RegisteTouchArea(okSprite);
                this.Attach(okSprite);
                okSprite.OnTouchEvent += OnOkTouch;
            }

            ButtonSprite homeSprite = new ButtonSprite();
            homeSprite.textureName = "p4/home";
            homeSprite.ZIndex = Constants.ui;
            homeSprite.Position = v2;
            homeSprite.OnLoad();
            TouchManager.RegisteTouchArea(homeSprite);
            this.Attach(homeSprite);
            homeSprite.OnTouchEvent += OnHomeTouch;
        }

        private void setTempData()
        {
            data.isWin = true;
            data.Score = 200;
            data.Speed = 300;
            data.Time = 20;
            data.Bouns = 0;
            data.StageNumber = 1;
        }

        private void setStageReward()
        {
            bounsText.Alpha = 0;
            timeText.Position = new Vector2(270, 241);
            scoreText.Position = new Vector2(270, 351);
            speedText.Position = new Vector2(270, 461);
            timeText.Value = "+" + timeValue;
            scoreText.Value = "+" + data.Score;
            speedText.Value = "+" + data.Speed;

            bounsSprite.Alpha = 0;
            timeSprite.Position = new Vector2(65, 240);
            scoreSprite.Position = new Vector2(65, 350);
            speedSprite.Position = new Vector2(65, 460);

            lineList[0].Position = new Vector2(240, 320);
            lineList[1].Position = new Vector2(240, 430);
        }

        void setBossReward()
        {
            scoreText.Alpha = 0;
            timeText.Position = new Vector2(270, 241);
            //scoreText.Position = new Vector2(270, 316);
            speedText.Position = new Vector2(270, 351);
            bounsText.Position = new Vector2(270, 461);

            timeText.Value = "+" + timeValue;
            //scoreText.Value = "+" + data.Score;
            speedText.Value = "+" + data.Speed;
            bounsText.Value = "+" + data.Bouns;

            scoreSprite.Alpha = 0;
            timeSprite.Position = new Vector2(65, 240);
            //scoreSprite.Position = new Vector2(65, 300);
            speedSprite.Position = new Vector2(65, 350);
            bounsSprite.Position = new Vector2(65, 460);

            lineList[0].Position = new Vector2(240, 320);
            lineList[1].Position = new Vector2(240, 430);
        }

        void makeTextEntity()
        {
            ResourceManager.ContentRoot = "";
            timeText = new TextEntity();
            timeText.fontString = "Century Gothic";
            timeText.OnLoad();
            timeText.Origin = new Vector2(0);
            timeText.Position = new Vector2(200, 50);
            timeText.Scale = new Vector2(1.5f);
            timeText.Color = Color.Black;
            timeText.ZIndex = Constants.debugText;
            Attach(timeText);

            scoreText = new TextEntity();
            scoreText.fontString = "Century Gothic";
            scoreText.OnLoad();
            scoreText.Origin = new Vector2(0);
            scoreText.Position = new Vector2(200, 50);
            scoreText.Scale = new Vector2(1.5f);
            scoreText.Color = Color.Black;
            scoreText.ZIndex = Constants.debugText;
            Attach(scoreText);

            speedText = new TextEntity();
            speedText.fontString = "Century Gothic";
            speedText.OnLoad();
            speedText.Origin = new Vector2(0);
            speedText.Position = new Vector2(200, 50);
            speedText.Scale = new Vector2(1.5f);
            speedText.Color = Color.Black;
            speedText.ZIndex = Constants.debugText;
            Attach(speedText);

            bounsText = new TextEntity();
            bounsText.fontString = "Century Gothic";
            bounsText.OnLoad();
            bounsText.Origin = new Vector2(0);
            bounsText.Position = new Vector2(200, 50);
            bounsText.Scale = new Vector2(1.5f);
            bounsText.Color = Color.Black;
            bounsText.ZIndex = Constants.debugText;
            Attach(bounsText);

            totalText = new TextEntity();
            totalText.fontString = "Century Gothic";
            totalText.OnLoad();
            totalText.Origin = new Vector2(0);
            totalText.Position = new Vector2(200, 50);
            totalText.Scale = new Vector2(1.5f);
            totalText.Color = new Color(new Vector3(164, 0, 0));
            totalText.ZIndex = Constants.debugText;
            Attach(totalText);
        }

        void OnHomeTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("role");
        }

        void OnOkTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            if (data.isWin)
            {
                switch (data.StageNumber)
                {
                    case 1:
                        Game1.stageManager.GoToScene("animationBoss1");
                        //Game1.stageManager.GoToScene("lifeStage1Boss");
                        break;
                    case 2:
                        Game1.stageManager.GoToScene("lifeStage2");
                        break;
                    case 3:
                        Game1.stageManager.GoToScene("animationBoss2");
                        //Game1.stageManager.GoToScene("lifeStage2Boss");
                        break;
                    case 4:
                        Game1.stageManager.GoToScene("animationEnd");
                        break;
                }
            }
            else
            {
                switch (data.StageNumber)
                {
                    case 1:
                        Game1.stageManager.GoToScene("lifeStage1");
                        break;
                    case 2:
                        Game1.stageManager.GoToScene("lifeStage1Boss");
                        break;
                    case 3:
                        Game1.stageManager.GoToScene("lifeStage2");
                        break;
                    case 4:
                        Game1.stageManager.GoToScene("lifeStage2Boss");
                        break;
                }
            }
        }

        public override BaseScene Clone()
        {
            BaseScene scene = new CalRewardScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
